#include "StdAfx.h"
#include "CGPSViewStatu.h"
#include "Misc.h"
#include "CGraphicMgr.h"
#include "CBackImg.h"
CGPSViewStatu::CGPSViewStatu(WCHAR* name):IStatu(name)
{
//	memset(m_pSlider,0,12*sizeof(CSlider));
//	memset(m_pStaictVid,0,12*sizeof(CStaticText));
//	memset(m_pStaictSN,0,12*sizeof(CStaticText));
	m_flag=false;
}

CGPSViewStatu::~CGPSViewStatu(void)
{
}

 bool CGPSViewStatu::EnterStatu()
 {
   
	 for(int i=0;i<NUM_CH;i++)
	 {

		 svInfo[i].azimuth = 0;
		 svInfo[i].elv = 0;
		 svInfo[i].Fix = 0;
		 svInfo[i].SNR = 0;
		 svInfo[i].SVid =0;
	 }

	 StaticImg *p= (StaticImg *)GRAPHICMGR.GetElement(L"SignalRC");
	m_rc.CopyRect(&(p->GetRect()));

	 p->setvisible(false);
	
	 for (int i=0;i<12;i++)
    {
		
		TCHAR strTemp[256]={0};
		swprintf(strTemp,L"data_slider%d",i);
		m_pSlider[i]=(CSlider*)GRAPHICMGR.GetElement(strTemp);
		m_pSlider[i]->SetRange(0,55);
		m_pSlider[i]->SetPos(0);

		swprintf(strTemp,L"data_slider_fixed%d",i);
		m_pSliderFixed[i]=(CSlider*)GRAPHICMGR.GetElement(strTemp);
		m_pSliderFixed[i]->SetRange(0,55);
		m_pSliderFixed[i]->SetPos(0);
		m_pSliderFixed[i]->setvisible(false);



		memset(strTemp,0,sizeof(TCHAR)*256);
		swprintf(strTemp,L"Text_Data%d",i);
		m_pStaictSN[i]=(CStaticText*)GRAPHICMGR.GetElement(strTemp);
		m_pStaictSN[i]->setvalue(L"");
	    memset(strTemp,0,sizeof(TCHAR)*256);
		swprintf(strTemp,L"Text_Vid%d",i);
		m_pStaictVid[i]=(CStaticText*)GRAPHICMGR.GetElement(strTemp);
		m_pStaictVid[i]->setvalue(L"");

    }
   return false;

 }
 bool CGPSViewStatu::ExitStatu()
 {
   
	 return false;
 }
 bool CGPSViewStatu::EvaluateClickEvent(const WCHAR* elementName)
 {

	 if (wcscmp(elementName,L"gpsview_btn_home") == 0)
	 {
		 PostMessage(GRAPHICMGR.GetWnd(),WM_CLOSE,0,0);
	 }

	 return false;
 }
 LRESULT CGPSViewStatu::OnGPSUpdate(WPARAM wParam, LPARAM lParam)
 {
     RefreshGPSInfo();
	 ShowSNRChart();
     ShowSkyChart();
	
	  return 0;
 }
 void CGPSViewStatu::RefreshGPSInfo()
 {
   CStaticText *p=(CStaticText*)GRAPHICMGR.GetElement(L"Text_Latitude");

  const WCHAR * lang = GlobalStatic::langGetItem(L"txtLatitude" ,GlobalStatic::langGetCurrentLang());
   TCHAR strTemp[256]={0};
   swprintf(strTemp,L"%s %0.6f",lang,g_gpsInfo.Lat);
   p->setvalue(strTemp);
   memset(strTemp,0,sizeof(TCHAR)*256);
   lang = GlobalStatic::langGetItem(L"txtLongitude" ,GlobalStatic::langGetCurrentLang());
   p=(CStaticText*)GRAPHICMGR.GetElement(L"Text_Longitude");
    swprintf(strTemp,L"%s %0.6f",lang,g_gpsInfo.Lon);
	p->setvalue(strTemp);
	memset(strTemp,0,sizeof(TCHAR)*256);
	p=(CStaticText*)GRAPHICMGR.GetElement(L"Text_Alitude");
	 lang = GlobalStatic::langGetItem(L"txtAltitude" ,GlobalStatic::langGetCurrentLang());
	swprintf(strTemp,L"%s %0.6f",lang,g_gpsInfo.Alt);
	p->setvalue(strTemp);
	memset(strTemp,0,sizeof(TCHAR)*256);
     p=(CStaticText*)GRAPHICMGR.GetElement(L"Text_Speed");
	  lang = GlobalStatic::langGetItem(L"txtSpeed" ,GlobalStatic::langGetCurrentLang());
    swprintf(strTemp,L"%s %0.6f",lang,g_gpsInfo.Speed);
    p->setvalue(strTemp);
    memset(strTemp,0,sizeof(TCHAR)*256);
    p=(CStaticText*)GRAPHICMGR.GetElement(L"Text_UTC");
	 lang = GlobalStatic::langGetItem(L"txtUTC" ,GlobalStatic::langGetCurrentLang());
	swprintf(strTemp,L"%s %04d-%02d-%02d% 02d:%02d:%02d",lang,g_gpsInfo.year,g_gpsInfo.mon,g_gpsInfo.day,\
		g_gpsInfo.hour,g_gpsInfo.min,(int)g_gpsInfo.sec);
	p->setvalue(strTemp);

   GRAPHICMGR.AllElementUpdate();
   GRAPHICMGR.Refresh();
 }
 void CGPSViewStatu::ShowSNRChart()
 {
	 int i,j;

	 int DispChnlNum;
	 int step;
	 double scale;
	 int MaxChannel;

	 if( g_fgSVUpdate)          //inview & SNR SV
	 {	
		 for( i=0,j=0 ; i<g_gpsInfo.SV_cnt ; i++)
		 {
			 
			 
		
			 if((g_svInfo[i].SVid >0) && (g_svInfo[i].SNR >0))
				 j++;
		 }
		 DispChnlNum=j;
		 if(DispChnlNum==0)               //no data 
			 return;
	 }


	 if(g_fgSVUpdate==true)                 //GSV cmd  
	 {
		 // UpdateRange=CRect(range.left,range.top-10,range.right,range.bottom+15);  //clear
		 // InvalidateRect(&UpdateRange,TRUE);
		 // UpdateWindow();

		 // step=range.Width()/(2*DispChnlNum);    




		 TCHAR tcTemp[12];
		 BOOL bFix = FALSE;
		 memset(&svInfo,0,20*sizeof(SVInfo));
		 for(i=0;i<g_gpsInfo.SV_cnt;i++)
		 {
			 if(g_svInfo[i].Fix)
			 {
				 bFix = TRUE;
			 }
			 svInfo[i].azimuth = g_svInfo[i].azimuth;
			 svInfo[i].elv = g_svInfo[i].elv;
			 svInfo[i].Fix = g_svInfo[i].Fix;
			 svInfo[i].SNR = g_svInfo[i].SNR;
			 svInfo[i].SVid = g_svInfo[i].SVid;
		 }

		 for(i=0;i<g_gpsInfo.SV_cnt;i++)
		 {
			 for (j = 0; j < g_gpsInfo.SV_cnt-1-i; j++)
			 {
				 if (svInfo[j].SNR < svInfo[j+1].SNR)
				 {
					 int a;	
					 unsigned char c;
					 a = svInfo[j+1].azimuth;	
					 svInfo[j+1].azimuth = svInfo[j].azimuth;
					 svInfo[j].azimuth = a;

					 a = g_svInfo[j+1].elv;
					 svInfo[j+1].elv = svInfo[j].elv;
					 svInfo[j].elv = a;

					 c = svInfo[j+1].Fix;
					 svInfo[j+1].Fix = svInfo[j].Fix;
					 svInfo[j].Fix = c;

					 a = svInfo[j+1].SNR;
					 svInfo[j+1].SNR = svInfo[j].SNR;	
					 svInfo[j].SNR = a;

					 a = svInfo[j+1].SVid;
					 svInfo[j+1].SVid = svInfo[j].SVid;
					 svInfo[j].SVid = a;

				 }
			 }
		 }




		 for( i=0; i<12; i++)
		 {
			 if((svInfo[i].SVid  >0) && (svInfo[i].SNR >0))
			 {	

				 if(bFix)
				 {
					/* m_Slider[i].SetImage(IDB_SLIDERBKG,
						 IDB_SLIDERFG,
						 NULL
						 );
                   
					 m_Slider[i].SetPos(svInfo[i].SNR);
					 TextOutVid(pDC,i,svInfo[i].SVid);*/
					 m_pSliderFixed[i]->setvisible(true);
					  m_pSlider[i]->setvisible(false);
					 m_pSliderFixed[i]->SetPos(svInfo[i].SNR);
					 TCHAR strTemp[256]={0};
					 swprintf(strTemp,L"%d",svInfo[i].SNR);
					 m_pStaictSN[i]->setvalue(strTemp);
					 memset(strTemp,0,sizeof(TCHAR)*256);
					 swprintf(strTemp,L"%d",svInfo[i].SVid);
					 m_pStaictVid[i]->setvalue(strTemp);
                    // GRAPHICMGR.AllElementUpdate();
					  // GRAPHICMGR.SetBgNeedUpdate(false)



				 }
				 else
				 {
					 m_pSliderFixed[i]->setvisible(false);
					 m_pSlider[i]->setvisible(true);
					 m_pSlider[i]->SetPos(svInfo[i].SNR);
					 TCHAR strTemp[256]={0};
					 swprintf(strTemp,L"%d",svInfo[i].SNR);
					 m_pStaictSN[i]->setvalue(strTemp);
					 memset(strTemp,0,sizeof(TCHAR)*256);
					 swprintf(strTemp,L"%d",svInfo[i].SVid);
					 m_pStaictVid[i]->setvalue(strTemp);
					// GRAPHICMGR.AllElementUpdate();
					//   GRAPHICMGR.SetBgNeedUpdate(false)

				 }

			 }
			 else
			 {
				 m_pSlider[i]->SetPos(0);
				m_pSliderFixed[i]->SetPos(0);
				 m_pStaictSN[i]->setvalue(L"");
				 m_pStaictVid[i]->setvalue(L"");
				 // GRAPHICMGR.AllElementUpdate();
				 // GRAPHICMGR.SetBgNeedUpdate(false)


			 }

		


		 }
		 // reset SVUPdate to false
		 g_fgSVUpdate = false;
	 }


 }
 void CGPSViewStatu::ShowSkyChart()
 {

	// CDC *dc=GetDC();
	// CRect range;
	 CRect SV,SVText;
	// CString id;
	 int i;
	 int CenterX, CenterY;
	 int X, Y;
	 double scaleX,scaleY;    //scaleX=scaleY


	 //range.top=59;
	 //range.left=16;
	 //range.right=163;
	 //range.bottom=208;

	 HDC dc=GRAPHICMGR.GetScreenDC();//GetDC(GRAPHICMGR.GetWnd());//GRAPHICMGR.GetScreenDC();//GetDC(NULL);
	 HDC Memdc  = CreateCompatibleDC(dc );
	 HBITMAP holdmap;
	 HBITMAP Membmp = CreateCompatibleBitmap(dc, m_rc.Width(), m_rc.Height());



	 holdmap=(HBITMAP)::SelectObject(Memdc, Membmp);

	 CRect rc;
	 rc.top=0;
	 rc.left=0;
	 rc.right=m_rc.Width();
	 rc.bottom=m_rc.Height();

	 CenterX = (rc.right+rc.left)/2;
	 CenterY = (rc.top+rc.bottom)/2;
	 scaleX=(rc.Width()/2)/90.0;
	 scaleY=(rc.Height()/2)/90.0;

	 //Font Color
	// dcMem.SetTextColor(RGB(200, 200, 0)); 
	// pOldBrush=dcMem.SelectObject(&br1);          //save old brush 
     HBRUSH hBrush=CreateSolidBrush(RGB(0, 0, 250));
	 SetTextColor(Memdc,RGB(200, 200, 0));
	 SetBkMode(Memdc,TRANSPARENT);
     HBRUSH oldBrush=(HBRUSH)SelectObject(Memdc,hBrush);
              //text bk transparent
	 StaticImg *p= (StaticImg *)GRAPHICMGR.GetElement(L"SignalRC");
	
     p->MoveTo(0,0);
	 p->setvisible(true);
	 p->draw(&Memdc);
	 wstring strID;
	 for(i=0;i<12 ; i++)
	 {
	 
		 if((svInfo[i].SVid  >0) && (svInfo[i].SNR >0))
		 {

			 X = CenterX;
			 Y = CenterY;

			 X += int(sin(svInfo[i].azimuth/180.0 * 3.14)*(90-svInfo[i].elv) * 0.8*scaleX);
			 Y -= int(cos(svInfo[i].azimuth/180.0 * 3.14)*(90-svInfo[i].elv) * 0.8*scaleY);

			 SV=CRect(X-10, Y-10, X+10, Y+10);
		      CRect rcTmp(X - 12 , Y - 12, X + 12, Y + 12);
              Ellipse(Memdc,SV.left,SV.top,SV.right,SV.bottom);
			
			  TCHAR tcId[4]={0};
			  wsprintf(tcId,L"%d",svInfo[i].SVid);
			  strID = tcId;
			  DrawText(Memdc,strID.c_str(),strID.size(),rcTmp,DT_CENTER|DT_SINGLELINE|DT_VCENTER);

		 }

	 }
	

   
	 BitBlt(dc,m_rc.left,m_rc.top, m_rc.Width(),  m_rc.Height(), Memdc,  0, 0, SRCCOPY);

	 SelectObject(Memdc,holdmap);
     SelectObject(Memdc,oldBrush);
	 DeleteObject(Membmp);
	 DeleteObject(Memdc);

     p->setvisible(false);


	

	 if (!m_flag)
	 {
		  GRAPHICMGR.SetBgNeedUpdate(true);
		  m_flag=true;
	 }
	 else
     GRAPHICMGR.SetBgNeedUpdate(true);


	
	// GRAPHICMGR.AllElementUpdate();
	 GRAPHICMGR.Refresh();

 }